package com.bjpowernode.oa.web.action;

import com.bjpowernode.oa.bean.Dept;
import com.bjpowernode.oa.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@WebServlet({"/dept/list","/dept/add","/dept/delete","/dept/detail","/dept/edit","/dept/modify"})
public class DeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取servlet路径
        String path = request.getServletPath();
        if("/dept/list".equals(path)){
            doList(request,response);
        }else if("/dept/add".equals(path)){
            doAdd(request,response);
        }else if("/dept/delete".equals(path)){
            doDel(request,response);
        }else if("/dept/detail".equals(path)){
            doDetail(request,response);
        }else if("/dept/edit".equals(path)){
            doEdit(request,response);
        }else{
            doModify(request,response);
        }
    }

    private void doModify(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String dname = request.getParameter("dname");
        String loc = request.getParameter("loc");
        String deptno = request.getParameter("deptno");
        String contextPath = request.getContextPath();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        int count=0;
        try {
            con= DBUtil.getConnection();
            String sql="update dept set dname=?,loc=? where deptno=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setString(1,dname);
            pstmt.setString(2,loc);
            pstmt.setInt(3,Integer.parseInt(deptno));
            count =pstmt.executeUpdate();
        } catch (SQLException exception) {
            exception.printStackTrace();
        }finally {
            DBUtil.close(con,pstmt,rs);
        }
        if(count==1){
            response.sendRedirect(request.getContextPath()+"/dept/list");
        }else{
            response.sendRedirect(request.getContextPath()+"/error.html");
        }
    }

    private void doEdit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String deptno = request.getParameter("deptno");
        //获取应用的根路径
        String contextPath = request.getContextPath();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {
            con= DBUtil.getConnection();
            String sql="select dname,loc from dept where deptno=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1,Integer.parseInt(deptno));
            rs=pstmt.executeQuery();
            if(rs.next()){
                String dname=rs.getString("dname");
                String loc=rs.getString("loc");

            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        }finally {
            DBUtil.close(con,pstmt,rs);
        }
    }

    private void doDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String deptno=request.getParameter("deptno");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {
            con= DBUtil.getConnection();
            String sql="select dname,loc from dept where deptno=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1,Integer.parseInt(deptno));
            rs=pstmt.executeQuery();
            if(rs.next()){
                String dname=rs.getString("dname");
                String loc=rs.getString("loc");
                Dept dept=new Dept(Integer.parseInt(deptno),dname,loc);
                request.setAttribute("dept",dept);
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        }finally {
            DBUtil.close(con,pstmt,rs);
        }
        String foward = "/"+request.getParameter("f")+".jsp";
        request.getRequestDispatcher(foward).forward(request,response);
    }

    private void doDel(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String deptno=request.getParameter("deptno");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        int count=0;
        try {
            con= DBUtil.getConnection();
            con.setAutoCommit(false);
            String sql="delete from dept where deptno=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1,Integer.parseInt(deptno));
            count=pstmt.executeUpdate();
            con.commit();
        } catch (SQLException e) {
            if(con!=null){
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        }finally {
            DBUtil.close(con,pstmt,rs);
        }
        if(count==1){
            response.sendRedirect(request.getContextPath()+"/dept/list");
        }else{
            response.sendRedirect(request.getContextPath()+"/error.jsp");
        }
    }

    private void doAdd(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String contextPath = request.getContextPath();
        response.setContentType("text/html;charset=utf-8");
        String deptno=request.getParameter("deptno");
        String dname=request.getParameter("dname");
        String loc=request.getParameter("loc");
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        int count=0;
        try {
            con= DBUtil.getConnection();
            String sql="insert into dept(deptno,dname,loc) values(?,?,?)";
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1,Integer.parseInt(deptno));
            pstmt.setString(2,dname);
            pstmt.setString(3,loc);
            count=pstmt.executeUpdate();
        } catch (SQLException exception) {
            exception.printStackTrace();
        }finally {
            DBUtil.close(con,pstmt,rs);
        }
        if(count==1){
            response.sendRedirect(request.getContextPath()+"/dept/list");
        }else{
            response.sendRedirect(request.getContextPath()+"/error.jsp");
        }
    }

    private void doList(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        //获取应用的根路径
        String contextPath = request.getContextPath();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        List<Dept> list=new ArrayList<>();
        try {
            con= DBUtil.getConnection();
            String sql="select deptno as a,dname,loc from dept";
            pstmt=con.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                String deptno=rs.getString("a");
                String dname=rs.getString("dname");
                String loc=rs.getString("loc");
                Dept dept=new Dept(Integer.parseInt(deptno),dname,loc);
                list.add(dept);
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        }finally {
            DBUtil.close(con,pstmt,rs);
        }
        request.setAttribute("deptList",list);
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }
}
